package de.bht.netviz.ipsaver.lookup.service;

import de.bht.netviz.ipsaver.lookup.config.IPSaverConfig;
import de.bht.netviz.ipsaver.lookup.config.LookupServiceControllerAdaptorFactory;
import de.bht.netviz.ipsaver.lookup.dao.ILookupServiceJpaController;
import de.bht.netviz.ipsaver.lookup.model.ILookupData;


/**
 * Abstract implementation of the ILookupService interface.
 * Does the connection to a specified XML-Web Service, sends the request and
 * takes the response.
 * 
 * @author Marius Hentschel
 * @since  2012/10/24
 */
public abstract class AbstractLookupService implements ILookupService {
   /** 
    * The URL of the current web service (set by the final implementation class).
	*/
   private final String baseURL;
   
   /**
    * Argument name of for the ip request
    */
   private final String argName;
   
   /**
    * Constructor sets the URL for this web service instance
	*/
   public AbstractLookupService( String baseURL, String argName ) {
      this.baseURL = baseURL;
      this.argName = argName;
   }
   
   /**
    * Does a lookup on the given IP Address. First, a lookup to the database is done.
    * If no information is available there, a lookup is requested from the web
    * service and saved to the database.
    * Then the concrete ILookupData object is returned.
    * 
    * @param ip
    *    The IP to do the lookup for, e.g. 192.168.1.100
    * @return 
    *    an ILookupData object, specific to the used web service, containing the
    *    result information. In case of an error it returns NULL.
    */
   @Override
   public ILookupData lookup(String ip) {
      ILookupData res = null;
      
      // check in database
      res = getFromDatabase( ip );
      
      // if not found, get it from the web service and save it to the database
      if( res == null ) {
         res = webserviceLookup( ip );
         if( res != null ) {
            ILookupService service = IPSaverConfig.getInstance().getCurrentLookupService();
            ILookupServiceJpaController dao = LookupServiceControllerAdaptorFactory.getController( service );
            dao.create(res);
//            IPSaverConfig.getInstance().getDAO().create( res );
         }
      } 
      
      //TODO: ggf. noch bei älteren Einträgen neu nachschlagen ?!
      return res;
   }
   
   /**
    * Tries to receive the lookup information from the database.
    * 
    * @param ip
    *    the IP to look up.
    * @return 
    *    if data is found, an ILookupData object is returned, otherwise NULL.
    */
   protected ILookupData getFromDatabase( String ip ) {
      return null;
      //TODO: Implement!
   }

   /**
    * Führt ein Lookup einer IP-Adresse über den implementierten Webservice durch.
    * 
    * @param content
    *    the XML returned by the web service call.
    * @return 
    *    the specific ILookupData object, or NULL, in case of an error.
    */
   abstract protected ILookupData webserviceLookup(String ip);

    protected String getBaseURL() {
        return baseURL;
    }

    protected String getArgName() {
        return argName;
    }
   
}
